package com.yuven.appframework.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日期时间格式化工具类
 */
public class DateFormat {

    public static final String DATA_FORMAT_DEFAULT1 = "yyyy-MM-dd HH:mm:ss";
    public static final String DATA_FORMAT_DEFAULT2 = "yyyy-MM-dd HH:mm";
    public static final String DATA_FORMAT_DEFAULT3 = "yyyy年MM月dd日 HH:mm";
    public static final String DATA_FORMAT_Y_M_D1 = "yyyy-MM-dd";
    public static final String DATA_FORMAT_Y_M_D2 = "yyyy/MM/dd";
    public static final String DATA_FORMAT_Y_M_D3 = "yyyyMMdd";
    public static final String DATA_FORMAT_HOUR_AND_MINUTE = "HH:mm";
    public static final String DATA_FORMAT_WEEK1 = "EEEE";
    public static final String DATA_FORMAT_WEEK2 = "E";
    public static final String DATA_FORMAT_14 = "yyyyMMddHHmmss";

    /**
     * 将日期格式化为星期
     */
    public static String dayOfWeek(Date target, String format) {
        java.text.DateFormat df = new SimpleDateFormat(format, Locale.getDefault());
        return df.format(target);
    }

    /**
     * 判断是否为昨天
     *
     * @param day 传入的时间
     * @return true今天 false不是
     */
    public static boolean isYesterday(Date day) {
        Calendar today = Calendar.getInstance();
        today.setTime(new Date(System.currentTimeMillis()));

        Calendar paramDay = Calendar.getInstance();
        paramDay.setTime(day);

        if (paramDay.get(Calendar.YEAR) == (today.get(Calendar.YEAR))) {
            int diffDay = paramDay.get(Calendar.DAY_OF_YEAR) - today.get(Calendar.DAY_OF_YEAR);
            return diffDay == -1;
        }
        return false;
    }

    /**
     * 判断两个日期是否是同一天
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean sameDay(Date date1, Date date2) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATA_FORMAT_Y_M_D1, Locale.getDefault());
        String format1 = sdf.format(date1);
        String format2 = sdf.format(date2);
        return format1.equals(format2);
    }

    /**
     * 判断两个日期是否在同一周内
     * 注:
     * Calendar类提供了一个获取日期在所属年份中是第几周的方法，对于上一年末的某一天
     * 和新年初的某一天在同一周内也一样可以处理，例如2012-12-31和2013-01-01虽然在
     * 不同的年份中，但是使用此方法依然判断二者属于同一周内
     */
    public static boolean sameWeek(Date date1, Date date2) {
        Calendar date1Calendar = Calendar.getInstance();
        Calendar date2Calendar = Calendar.getInstance();

        date1Calendar.setTime(date1);
        date2Calendar.setTime(date2);

        //比较两个日期在年份中的周数是否相同
        return date1Calendar.get(Calendar.WEEK_OF_YEAR) == date2Calendar.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * Get current time by giving format
     */
    public static String getCurrentTimeString(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
        return sdf.format(new Date());
    }

    // time要转换的long类型的时间
    // formatType要转换的string类型的时间格式
    public static String longToString(long time, String formatType) {
        Date date = null; // long类型转成Date类型
        try {
            date = longToDate(time, formatType);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateToString(date, formatType);
    }

    // time要转换的long类型的时间
    // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    public static Date longToDate(long time, String formatType) throws ParseException {
        Date dateOld = new Date(time); // 根据long类型的毫秒数生命一个date类型的时间
        String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string
        return stringToDate(sDateTime, formatType);
    }

    // strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒，
    // strTime的时间格式必须要与formatType的时间格式相同
    public static Date stringToDate(String strTime, String formatType) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType, Locale.getDefault());
        return formatter.parse(strTime);
    }

    // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
    // data Date类型的时间
    public static String dateToString(Date data, String formatType) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatType, Locale.getDefault());
        return sdf.format(data);
    }

    //秒数转换成x天x小时
    public static String formatSecToDayHour(long timeSec) {
        long day = 0;
        long hour = 0;
        hour = timeSec / 60 / 60;
        if (hour == 0) {    //1h小时内用0天1小时代替
            day = 0;
            hour = 1;
        } else {
            if (hour >= 24) {
                day = hour / 24;
                hour = hour % 24;
            } else {
                day = 0;
            }
        }
        return day + "天" + hour + "小时";
    }

    private static String unitFormat(long i) {
        String retStr = "";
        if (i >= 0 && i < 10)
            retStr = "0" + i;
        else
            retStr = "" + i;
        return retStr;
    }

    // a integer to xx时xx分 整数(秒数)转换为时分格式(xx时xx分)
    public static String secToTime(long time) {
        String timeStr;
        long hour;
        long minute;
        if (time <= 0)
            return "0分";
        else {
            hour = time / 60 / 60;
            if (hour > 9999) hour = 9999;
            minute = (time - hour * 3600) / 60;
            if (hour == 0) {
                if (minute != 0)
                    timeStr = minute + "分";
                else
                    timeStr = "1分";//不足1分钟，按1分钟计
            } else {
                timeStr = hour + "时" + minute + "分";
            }
        }
        return timeStr;
    }

    // a integer to xx:xx:xx 整数(秒数)转换为天时分秒格式(x天xx:xx:xx)
    public static String secToDayTime(long timeSec) {
        String timeStr = "";
        long day = 0;
        long hour = 0;
        long minute = 0;
        long second = 0;
        if (timeSec <= 0)
            return "00:00:00";
        else {
            day = timeSec / (60 * 60 * 24);
            hour = (timeSec % (60 * 60 * 24)) / (60 * 60);
            minute = (timeSec % (60 * 60)) / 60;
            second = timeSec % 60;
            if (day > 0) {
                timeStr = day + "天" + unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            } else if (hour == 0 && minute != 0) {
                timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second);
            } else if (hour == 0 && minute == 0) {
                timeStr = "00:00:" + unitFormat(second);
            } else {
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    // a integer to xx:xx:xx 整数(秒数)转换为天时分秒格式(x天xx小时)
    public static String secToDayTime1(long timeSec) {
        long day = 0;
        long hour = 0;
        long minute = 0;
        if (timeSec <= 0)
            return "0分钟";
        else {
            day = timeSec / (60 * 60 * 24);
            hour = (timeSec % (60 * 60 * 24)) / (60 * 60);
            minute = (timeSec % (60 * 60)) / 60;
            if (day == 0) {
                if (minute == 0)
                    return hour + "小时";
                return hour + "小时" + minute + "分钟";
            }
        }
        return day + "天" + hour + "小时";
    }

    // a integer to xx:xx:xx 整数(秒数)转换为时分秒格式(xx:xx:xx)
    public static String secToDayTime2(long timeSec) {
        String timeStr = "";
        long hour = 0;
        long minute = 0;
        long second = 0;
        if (timeSec <= 0)
            return "00:00:00";
        else {
            hour = (timeSec % (60 * 60 * 24)) / (60 * 60);
            minute = (timeSec % (60 * 60)) / 60;
            second = timeSec % 60;
            if (hour == 0 && minute != 0) {
                timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second);
            } else if (hour == 0 && minute == 0) {
                timeStr = "00:00:" + unitFormat(second);
            } else {
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    /**
     * 今天内->直接返回 具体时间 eg:08:30
     * 昨天内->直接返回 昨天+时间 eg:昨天 08:30
     * 同一周内->直接返回 星期几+时间 eg:周一 08:30
     * 一周之前->直接返回 年月日+时间 eg:2022年05月15日 08:30
     */
    public static String formatCallLogTime(long time) {
        Date dateToday = new Date();
        Date dateParam = new Date(time);
        if (sameDay(dateToday, dateParam)) {
            SimpleDateFormat sdf = new SimpleDateFormat(DATA_FORMAT_HOUR_AND_MINUTE, Locale.getDefault());
            return sdf.format(dateParam);
        } else if (isYesterday(dateParam)) {
            SimpleDateFormat sdf = new SimpleDateFormat(DATA_FORMAT_HOUR_AND_MINUTE, Locale.getDefault());
            return "昨天 " + sdf.format(dateParam);
        } else if (sameWeek(dateToday, dateParam)) {
            SimpleDateFormat sdf = new SimpleDateFormat(DATA_FORMAT_HOUR_AND_MINUTE, Locale.getDefault());
            return dayOfWeek(dateParam, DATA_FORMAT_WEEK2) + " " + sdf.format(dateParam);
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(DATA_FORMAT_DEFAULT3, Locale.getDefault());
            return sdf.format(dateParam);
        }
    }

    public static String formatDate(long time, String formatType) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatType);
        return sdf.format(time);
    }
}
